﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QLK.Database.DALs
{
    public class Transfer_ProductDAL
    {
        private ManageStoreDataContext db;

        public Transfer_ProductDAL()
        {
            db = new ManageStoreDataContext();
        }

        public List<Transfer_Product> Transfer_ProductGetAll()
        {
            var TP = from tp in db.Transfer_Products
                     orderby tp.TotalValues
                     select tp;

            return TP.ToList<Transfer_Product>();
        }

        public Transfer_Product Transfer_ProductGetByID(int TransID, int ProID)
        {
            Transfer_Product TP = db.Transfer_Products.Single<Transfer_Product>(tp => tp.TransferID == TransID
                && tp.ProductID == ProID);

            return TP;
        }

        public bool Transfer_ProductDeleteByID(int TransID, int ProID)
        {
            Transfer_Product TP = db.Transfer_Products.Single<Transfer_Product>(tp => tp.TransferID == TransID
                && tp.ProductID == ProID);

            if (TP == null)
                return false;

            db.Transfer_Products.DeleteOnSubmit(TP);

            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

        public bool Transfer_ProductUpdate(Transfer_Product tps)
        {
            Transfer_Product TP = db.Transfer_Products.Single<Transfer_Product>(tp => tp.TransferID == tps.TransferID
                && tp.ProductID == tps.ProductID);

            if (TP == null)
                return false;

            TP.Price = tps.Price;
            TP.Quantity = tps.Quantity;
            TP.TotalValues = tps.TotalValues;

            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

        public bool Transfer_ProductInsert(Transfer_Product tps)
        {
            try
            {
                db.Transfer_Products.InsertOnSubmit(tps);
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

        public bool Transfer_ProductCheckDuplicateProduct(int TransID, int ProID)
        {
            var T = from t in db.Transfer_Products
                    select t;

            if (T.ToList<Transfer_Product>().Count == 0)
                return true;
            else
            {
                Transfer_Product TP = db.Transfer_Products.Single<Transfer_Product>(tp => tp.TransferID == TransID
                    && tp.ProductID == ProID);

                if (TP == null)
                    return true;
                return false;
            }
        }
    }
}
